﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Controls;
using LMD.Lib.Oracle;

public partial class FeedBlocking : ICPGeneralPage
{
    public DataTable m_dtCustomer = new DataTable();
    public DataTable m_dtUser = new DataTable();

    #region Properties
    #endregion Properties

    #region Event Handlers
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Pengaturan Blocking News Feed";
        InitGrid(!IsPostBack && !IsCallback);
    }
    
    protected void dgCustomer_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        UpdateCustomerFeedBlocking();
    }

    protected void dgUser_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        UpdateUserFeedBlocking();
    }
    #endregion Event Handlers

    #region Custom Methods
    private void InitGrid(bool bRefresh)
    {
        Cached.FeedBlocking ccTmp = new Cached.FeedBlocking();
        
        m_dtCustomer = ccTmp.GetCustomerFeedBlockingList(m_sxUID, bRefresh);
        dgCustomer.Caption = "ICP Feed Blocking by Customer";
        dgCustomer.DataSource = m_dtCustomer;
        dgCustomer.DataBind();

        m_dtUser = ccTmp.GetUserFeedBlockingList(m_sxUID, bRefresh);
        dgUser.Caption = "ICP Feed Blocking by User";
        dgUser.DataSource = m_dtUser;
        dgUser.DataBind();

    }

    private void UpdateCustomerFeedBlocking()
    {
        DataTable dtUpdate = (DataTable)dgCustomer.DataSource;

        DBLib dbl = new DBLib();

        dbl.BeginTransaction();
        try
        {
            for (int i = 0; i < dtUpdate.Rows.Count; i++)
            {
                bool IsSelected = dgCustomer.Selection.IsRowSelectedByKey(dtUpdate.Rows[i]["ID"]);

                if (IsSelected)
                {
                    int iCustomerId = dtUpdate.Rows[i].ReadInt("ID");
                    DBQuery.NewsFeedTable.UnblockNewsFeed_FromCustomer(dbl, iCustomerId);
                }
            }

            dbl.EndTransaction(true);

            InitGrid(true);
        }
        catch (Exception ex)
        {
            dbl.EndTransaction(false);
            throw ex;
        }
    }

    private void UpdateUserFeedBlocking()
    {
        DataTable dtUpdate = (DataTable)dgUser.DataSource;

        DBLib dbl = new DBLib();

        dbl.BeginTransaction();
        try
        {
            for (int i = 0; i < dtUpdate.Rows.Count; i++)
            {
                bool IsSelected = dgUser.Selection.IsRowSelectedByKey(dtUpdate.Rows[i]["ID"]);

                if (IsSelected)
                {
                    String sxUserId = dtUpdate.Rows[i].ReadString("ID");
                    DBQuery.NewsFeedTable.UnblockNewsFeed_FromUser(dbl, sxUserId);
                }
            }

            dbl.EndTransaction(true);

            InitGrid(true);
        }
        catch (Exception ex)
        {
            dbl.EndTransaction(false);
            throw ex;
        }
    }


    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    #endregion Custom Methods

    
}
